import { type RouteRecordRaw, createRouter } from "vue-router"
import { history, flatMultiLevelRoutes } from "./helper"
import routeSettings from "@/config/route"

const Layouts = () => import("@/layouts/index.vue")

/**
 * 常驻路由
 * 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/",
    component: Layouts,
    redirect: "/sqlsearch",
    children: [
      {
        path: "sqlsearch",
        component: () => import("@/views/sqlsearch/index.vue"),
        name: "sqlsearch",
        meta: {
          title: "SQL查询助手",
          svgIcon: "searchsql",
          affix: true
        }
      }
    ]
  }
]

/**
 * 动态路由
 * 用来放置有权限 (Roles 属性) 的路由
 * 必须带有 Name 属性
 */
export const dynamicRoutes: RouteRecordRaw[] = [
  {
    path: "/question",
    component: Layouts,
    redirect: "/question/index",
    meta: {
      roles: ["admin"]
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/question/index.vue"),
        name: "Sqlsearch",
        meta: {
          title: "推荐问题",
          svgIcon: "question"
        }
      }
    ]
  },
  {
    path: "/dataconfig",
    component: Layouts,
    redirect: "/dataconfig/index",
    meta: {
      roles: ["admin"]
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/dataconfig/index.vue"),
        name: "Dataconfig",
        meta: {
          title: "数据库配置",
          svgIcon: "sqlset"
        }
      }
    ]
  },
  {
    path: "/dbconnect",
    component: Layouts,
    redirect: "/dbconnect/index",
    meta: {
      roles: ["admin"]
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/dbconnect/index.vue"),
        name: "Dbconnect",
        meta: {
          title: "数据库连接",
          svgIcon: "connect"
        }
      }
    ]
  },
  {
    path: "/knowledge",
    component: Layouts,
    redirect: "/knowledge/index",
    meta: {
      roles: ["admin"]
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/knowledge/index.vue"),
        name: "Knowledge",
        meta: {
          title: "知识库",
          svgIcon: "knowledge"
        }
      }
    ]
  },
  {
    path: "/approval",
    component: Layouts,
    redirect: "/approval/index",
    meta: {
      roles: ["admin"]
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/knowledge/approval.vue"),
        name: "ApprovalList",
        meta: {
          title: "用户反馈审核",
          svgIcon: "approval"
        }
      }
    ]
  },
  {
    path: "/review",
    component: Layouts,
    redirect: "/review/index",
    meta: {
      roles: ["admin"]
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/review/index.vue"),
        name: "ReviewList",
        meta: {
          title: "用户注册审核",
          svgIcon: "userRegister"
        }
      }
    ]
  }
]

const router = createRouter({
  history,
  routes: routeSettings.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
  try {
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    window.location.reload()
  }
}

export default router
